package com.why.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.servicebase.exception.GuilException;
import com.why.serviceedu.client.VodClient;
import com.why.serviceedu.mapper.EduVideoMapper;
import com.why.serviceedu.service.EduVideoService;
import com.why.serviceedu.bean.EduVideo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-02-01
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    @Autowired
   private VodClient client;
    /**
     * // TODO: 2021/2/5 仅仅删除了小节，但是小节对应的视频还未删除
     * @param courseId
     */
    @Override
    public void removeVideoByCourseId(String courseId) {
        //1.根据课程id查询所有的小节对应的视频id
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        //查询指定列
        wrapperVideo.select("video_source_id");
        List<EduVideo> videos = baseMapper.selectList(wrapperVideo);
        //添加
        List<String> ids=new ArrayList<>();
        for(EduVideo video:videos){
            //不是空才删除
            if(!StringUtils.isEmpty(video)){
                ids.add(video.getVideoSourceId());
            }
        }
        //长度大于0才进行删除
        if(ids.size()>0){
            client.deleteVideos(ids);
        }
        //2. 删除所有的小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int delete = baseMapper.delete(wrapper);
        if(delete<=0)
            throw  new GuilException(20001,"小节删除失败！");
    }
}
